package org.scboy.dbms.dubbo;

import java.util.List;
import java.util.Map;

import org.apache.dubbo.config.annotation.DubboService;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.api.IPermissionManager;
import org.scboy.dbms.dao.BaseAppPermissionDao;
import org.scboy.dbms.loads.LoadSystemConfig;
import org.scboy.dbms.service.BaseAppPermissionService;
import org.scboy.utils.caches.dbms.entity.PermissionEntity;
import org.scboy.utils.caches.dbms.entity.SystemConfigEntity;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class IPermissionManagerImpl implements IPermissionManager {

	@Autowired
	EntityFactory factory;
	
	@Autowired
	BaseAppPermissionService server;
	
	@Autowired
	LoadSystemConfig systemConfigCache;
	
	@Override
	public void updatePermission(List<Map<String, String>> permissions, String appName) {
		server.updatePermission(permissions, appName);
	}

	@Override
	public String getSystemConfigValue(String code) {
		SystemConfigEntity config=systemConfigCache.get(code);
		if(config!=null) {
			return config.getValue();
		}else {
			return "";
		}
	}

	@Override
	public SystemConfigEntity getSystemConfig(String code) {
		SystemConfigEntity config=systemConfigCache.get(code);
		return config;
	}

	@Override
	public PermissionEntity getPermission(String appName,String path) {
		BaseAppPermissionDao dao=factory.getDao(BaseAppPermissionDao.class);
		Model data=dao.get(Model.create().add("appName", appName).add("persmissionuUrl", path));
		if(data!=null) {
			return data.toEntity(PermissionEntity.class);
		}else {
			return null;
		}
	}
	
}
